package com.jijuxie.framework.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private UploadConfig uploadConfig;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 映射图片访问路径到实际存储路径 - 修正路径映射问题
        String path = uploadConfig.getPath();
        // 确保路径以斜杠结尾
        if (!path.endsWith("/") && !path.endsWith("\\")) {
            path = path + "/";
        }
        // 确保file:路径格式正确
        if (!path.startsWith("file:")) {
            path = "file:" + path;
        }
        
        // 日志输出实际的映射路径
        System.out.println("静态资源映射: /profile/** -> " + path);
        
        registry.addResourceHandler("/profile/**")
                .addResourceLocations(path);
    }
}
